<?php

  /*
    注册 后端
  */

  $username = $_POST['username'];
  $password = $_POST['password'];
  $nickname = $_POST['nickname'];

  $sql = "SELECT * FROM `users` WHERE `username`='$username'";

  $link = mysqli_connect('localhost', 'root', 'root', 'dhsc');

  $res = mysqli_query($link, $sql);

  $data = mysqli_fetch_all($res, MYSQLI_ASSOC);

  if (count($data)) {

    $arr = [
      "message" => "注册失败, 该用户名已经被占用",
      "code" => 2
    ];

    echo json_encode($arr);

    exit;
  }

  $sql2 = "INSERT INTO `users` (`username`, `password`, `nickname`) VALUES('$username', '$password', '$nickname')";

  $res2 = mysqli_query($link, $sql2);

  if ($res2) {
    $arr = [
      "message" => "注册成功",
      "code" => 1
    ];
  } else {
    $arr = [
      "message" => "注册失败, 请联系管理员",
      "code" => 0
    ];
  }

  echo json_encode($arr);

?>
